StupidBeauty
Read times:3100Posted at:Tue Aug 4 03:19:52 2020 - no title specified

不同编程语言中的else if语法关键字

概述

常用的编程语言一般都会提供else if语法,一般这个语法的关键字也就是“else if”串联组合在一起。然而某些语言的else if关键字却很奇怪,比如说Ruby的就是elsif。

以下列出所有之前用过并且记得自己用过的语言中的else if关键字,并且给出一段代码示例。

Golang

else if

这应该是大部分人所预期的语法了。

package main

import (

"fmt"

)

func main() {

x := 100

if x == 50 {

fmt. Println ( "Germany" )

} else if x == 100 {

fmt. Println ( "Japan" )

} else {

fmt. Println ( "Canada" )

}

}

Unix Shell

elif

Ruby的相比还是觉得正常一点。

#!/bin/sh

a = 10

b = 20

if [ $a == $b ]

then

echo "a is equal to b"

elif [ $a - gt $b ]

then

echo "a is greater than b"

elif [ $a - lt $b ]

then

echo "a is less than b"

else

echo "None of the condition met"

fi

Java

else if

符合一般习惯

int time = 22 ;

if ( time < 10 ) {

System . out . println ( "Good morning." );

} else if ( time < 20 ) {

System . out . println ( "Good day." );

} else {

System . out . println ( "Good evening." );

}

// Outputs "Good evening."

C++

else if

符合一般习惯。

int time =  22 ;
if (time <  10 ) {
cout <<  "Good morning." ;
else if (time <  20 ) {
cout <<  "Good day." ;
else {
cout <<  "Good evening." ;
}
// Outputs "Good evening."

Ruby

elsif

就因为这么奇怪,才想到比较一下不同语言的关键字。

#!/usr/bin/ruby

x = 1

if x > 2

puts "x is greater than 2"

elsif x <= 2 and x != 0

puts "x is 1"

else

puts "I can't guess the number"

end

Python

elif

跟Shell的一样。

a =  33
b =  33
if b > a:
print ( "b is greater than a" )
elif a == b:
print ( "a and b are equal" )

JavaScript

else if

符合一般习惯

<html>

<head>

 <script type="text/javascript">

 var one = prompt("Enter the first number");

 var two = prompt("Enter the second number");

 one = parseInt(one);

 two = parseInt(two);

 if (one == two)

 document.write(one + " is equal to " + two + ".");

 else if (one<two)

 document.write(one + " is less than " + two + ".");

 else

 document.write(one + " is greater than " + two + ".");

 </script>

</head>

<body>

</body>

</html>

C#

else if

符合一般习惯

using System;

namespace Conditional

{

class IfElseIfStatement

{

public static void Main ( string [] args)

{

int number = 12 ;

if (number < 5 )

{

Console.WriteLine( "{0} is less than 5" , number);

}

else if (number > 5 )

{

Console.WriteLine( "{0} is greater than 5" , number);

}

else

{

Console.WriteLine( "{0} is equal to 5" );

}

}

}

}

Lua

elseif

else if粘在一起写,也还算直观的。

--[[
In Lua, if, elseif and else use then and end
clauses to delimit the conditional code
]]


j = 45

-- == for equality testing
if j==45 then print ("yes") end
if j==47 then print ("no") end

-- ~= for NOT equals
if j~=47 then print ("two nose make a yes") end

-- chaining with elseif and endif
if j<40 then print ("small") elseif j>50 then print ("big") else print ("between") end

-- much better to write it as blocks down the screen!
range = 7
if j<40 then
print ("tiny")
range = range + 2
elseif j>50 then
print ("huge")
range = range + 6
else
print ("middlin")
range = range + 4
end
print ("range is " .. range)

--[[ ----------------------------- Sample Output -------------
[trainee@easterton u103]$ lua lif
yes
two nose make a yes
between
middlin
range is 11
[trainee@easterton u103]$
]]

Rust

else if

符合一般习惯

fn main() {

    // All have type `Option<i32>`

    let number = Some(7);

    let letter: Option<i32> = None;

    let emoticon: Option<i32> = None;

    // The `if let` construct reads: "if `let` destructures `number` into

    // `Some(i)`, evaluate the block (`{}`).

    if let Some(i) = number {

        println!("Matched {:?}!", i);

    }

    // If you need to specify a failure, use an else:

    if let Some(i) = letter {

        println!("Matched {:?}!", i);

    } else {

        // Destructure failed. Change to the failure case.

        println!("Didn't match a number. Let's go with a letter!");

    }

    // Provide an altered failing condition.

    let i_like_letters = false;

    if let Some(i) = emoticon {

        println!("Matched {:?}!", i);

    // Destructure failed. Evaluate an `else if` condition to see if the

    // alternate failure branch should be taken:

    } else if i_like_letters {

        println!("Didn't match a number. Let's go with a letter!");

    } else {

        // The condition evaluated false. This branch is the default:

        println!("I don't like letters. Let's go with an emoticon :)!");

    }

}

R

else if

符合一般习惯

# Create vector quantiy

quantity <-  10

# Create multiple condition statement

if (quantity <20) {

print('Not enough for today')

} else if (quantity > 20  &quantity <= 30) {

print('Average day')

} else {

print('What a great day!')

}

Perl

elsif

跟Ruby的一样。

print "Enter salary\n" ;

$a = <>;

chomp ( $a );

print "Enter sex M/F\n" ;

$b = <>;

chomp ( $b );

if ( $a > 10000 && $b eq "M" ){

print "Your salary is good and you are a male\n" ;

}

elsif ( $a > 10000 && $b eq "F" ){

print "Your salary is good and you are a female\n" ;

}

elsif ( $a <= 10000 && $b eq "M" ){

print "Your salary is not so good and you are a male\n" ;

}

elsif ( $a <= 10000 && $b eq "F" ){

print "Your salary is not so good and you are a female\n" ;

}

else {

print "Enter valid input\n" ;

}

汇率

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands

 
Recent comments
2017年4月~2019年4月垃圾短信排行榜Posted at:Thu Sep 26 04:51:48 2024
Qt5.7文档翻译:QWebEngineCookieStore类,QWebEngineCookieStore ClassPosted at:Fri Aug 11 06:50:35 2023盲盒kill -9 18289 Grebe.20230517.211749.552.mp4